package cn.com.chec.en.controller.admin;

import cn.com.chec.en.dao.UserInfoDao;
import cn.com.chec.en.model.domain.RightInfo;
import cn.com.chec.en.model.domain.RoleInfo;
import cn.com.chec.en.model.domain.UserInfo;
import cn.com.chec.en.util.PageBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({ "/admin/role" })
public class RoleAdminController {
	protected static final Logger logger = Logger
			.getLogger(RoleAdminController.class);

	@Resource
	private UserInfoDao userInfoDao;

	@Resource
	private Map<String, Object> environment;

	@SuppressWarnings("unchecked")
	@RequestMapping({ "/showInsert" })
	public ModelAndView showInsert(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/userInfo/roleInfoOpr");

		List<String> roleRights = new ArrayList<String>();
		RoleInfo roleInfo = new RoleInfo();
		map.put("act", "insert");
		map.put("roleRights", roleRights);
		map.put("roleInfo", roleInfo);
		map.put("userRightsSession",
				(List<String>) session.getAttribute("systemUserRigthList"));

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/insert" })
	public ModelAndView insert(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		RoleInfo roleInfo = new RoleInfo();
		roleInfo.setRoleName(request.getParameter("roleName"));
		roleInfo.setRoleDesc(request.getParameter("roleDesc"));
		String[] rightValues = request.getParameterValues("rightValue");
		List<RightInfo> list = new ArrayList<RightInfo>();
		RightInfo ri = null;
		for (String s : rightValues) {
			ri = new RightInfo();
			ri.setRightValue(s);
			list.add(ri);
		}
		if (this.userInfoDao.createRole(roleInfo, list))
			message = "创建角色成功!";
		else {
			message = "创建角色失败!";
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping({ "/showUpdate" })
	public ModelAndView showUpdate(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/userInfo/roleInfoOpr");
		int roleId = StringUtils.isNotBlank(request.getParameter("roleId")) ? Integer
				.parseInt(request.getParameter("roleId")) : 0;
		RoleInfo roleInfo = this.userInfoDao.getRoleInfoByRoleId(roleId);

		List<String> roleRights = this.userInfoDao
				.getRightsStringByRoleId(roleId);
		map.put("roleInfo", roleInfo);
		map.put("roleRights", roleRights);
		map.put("userRightsSession",
				(List<String>) session.getAttribute("systemUserRigthList"));
		map.put("act", "update");

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/update" })
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/common/message");
		String message = "";
		int roleId = StringUtils.isNotBlank(request.getParameter("roleId")) ? Integer
				.parseInt(request.getParameter("roleId")) : 0;
		RoleInfo roleInfo = new RoleInfo();
		roleInfo.setRoleId(roleId);
		roleInfo.setRoleName(request.getParameter("roleName"));
		roleInfo.setRoleDesc(request.getParameter("roleDesc"));
		String[] rightValues = request.getParameterValues("rightValue");
		List<RightInfo> list = new ArrayList<RightInfo>();
		RightInfo ri = null;
		for (String s : rightValues) {
			ri = new RightInfo();
			ri.setRightValue(s);
			list.add(ri);
		}
		if (this.userInfoDao.updateRole(roleInfo, list)) {
			message = "更新角色成功!";

			UserInfo userInfoNewSession = this.userInfoDao
					.getUser(((UserInfo) session.getAttribute("systemUserInfo"))
							.getId());
			if (roleId == 1) {
				session.removeAttribute("systemUserInfo");
				session.setAttribute("systemUserInfo", userInfoNewSession);
			}
		} else {
			message = "更新角色失败!";
		}

		map.put("message", message);

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/list" })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, ModelMap map) throws Exception {
		ModelAndView mav = new ModelAndView("admin/userInfo/roleList");
		int curPage = StringUtils.isNotBlank(request.getParameter("curPage")) ? Integer
				.parseInt(request.getParameter("curPage")) : 1;
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<RoleInfo> roleInfoList = this.userInfoDao.getRoleList(
				pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(this.userInfoDao.getRoleCount());
		pageBean.setMaxPage();
		pageBean.setPageNoList();
		map.put("roleInfoList", roleInfoList);
		map.put("pageBean", pageBean);

		map.putAll(this.environment);
		mav.addAllObjects(map);
		return mav;
	}
}